<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_right">
          <div>
            <el-button
              v-if="isAuth('module:business:archived:regular:delete')"
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.shipowner_id"
            :placeholder="i18n('common.please_select') + i18n('business.regular.shipowner_id')"
             clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in shipownerList" :label="v.chinese_name" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-input v-model="dataForm.title" :placeholder="$t('common.please_input') + $t('business.regular.title')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.status" :placeholder="$t('common.please_select') + $t('common.status')" clearable>
            <el-option :label="$t('common.all')" value=""></el-option>
            <el-option :label="$t('common.enable')" value="1"></el-option>
            <el-option :label="$t('common.disable')" value="2"></el-option>
          </el-select>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.publish_time" type="daterange" :range-separator="$t('common.to')" :start-placeholder="$t('common.start_time')" :end-placeholder="$t('common.end_time')" clearable>
          </el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" label="#" width="70px" type="expand">
            <template v-slot="scope">
              <div style="margin: 0 70px;">
                <el-card class="box-card" shadow="never">
                  <template v-slot:header>
                    <div class="clearfix">
                      <span>{{ i18n('common.basic_info') }}</span>
                    </div>
                  </template>
                  <div class="text item">
                    <el-descriptions border>
                      <!-- 第1行 -->
                      <el-descriptions-item width="200" :label="i18n('business.regular.title')" label-align="right">
                        {{ scope.row.title }}
                      </el-descriptions-item>
                      <el-descriptions-item width="200" :label="i18n('business.regular.voyage_number')" label-align="right">
                        {{ scope.row.voyage_number }}
                      </el-descriptions-item>
                      <el-descriptions-item width="200" :label="i18n('common.create_time')" label-align="right">
                        {{ scope.row.create_time }}
                      </el-descriptions-item>

                      <!-- 第2行 -->
                      <el-descriptions-item width="200" :label="i18n('business.regular.category_id')" label-align="right">
                        <span v-if="scope.row.category">{{ scope.row.category.chinese_title }}</span>
                      </el-descriptions-item>
                      <el-descriptions-item width="200" :label="i18n('business.regular.machine_id')" label-align="right">
                        <span v-if="scope.row.machine">{{ scope.row.machine.chinese_title }}</span>
                      </el-descriptions-item>
                      <el-descriptions-item width="200" :label="i18n('business.regular.publish_time')" label-align="right">
                        {{ scope.row.publish_time }}
                      </el-descriptions-item>

                      <!-- 第3行 -->
                      <!-- <el-descriptions-item width="200" :label="i18n('business.regular.source')" label-align="right" :span="3">
                        <span v-if="scope.row.remark">{{ scope.row.remark.source }}</span>
                      </el-descriptions-item>D -->

                      <!-- 第4行 -->
                      <el-descriptions-item width="200" :label="i18n('business.regular.disclaimer')" label-align="right" :span="3">
                        <span v-if="scope.row.remark">{{ scope.row.remark.disclaimer }}</span>
                      </el-descriptions-item>

                      <!-- 第5行 -->
                      <el-descriptions-item width="200" :label="i18n('business.regular.remark')" label-align="right" :span="3">
                        <span v-if="scope.row.remark">{{ scope.row.remark.remark }}</span>
                      </el-descriptions-item>
                    </el-descriptions>
                  </div>
                </el-card>

                <el-card class="box-card mt10" shadow="never" v-if="scope.row.shipowner">
                  <template v-slot:header>
                    <div class="clearfix">
                      <span>{{ i18n('business.container.schedule.shipowner_info') }}</span>
                    </div>
                  </template>
                  <div class="text item">
                    <el-row>
                      <el-col :span="4">
                        <el-image class="cx-view-preview" :src="scope.row.shipowner.logo">
                          <template v-slot:error>
                            <div class="image-slot">
                              <el-icon><Picture /></el-icon>
                            </div>
                          </template>
                        </el-image>
                      </el-col>
                      <el-col :span="20">
                        <el-descriptions border>
                          <!-- 第1行 -->
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.scac')" label-align="right">
                            {{ scope.row.shipowner.scac }}
                          </el-descriptions-item>
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.chinese_name')" label-align="right">
                            {{ scope.row.shipowner.chinese_name }}
                          </el-descriptions-item>
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.english_name')" label-align="right">
                            {{ scope.row.shipowner.english_name }}
                          </el-descriptions-item>

                          <!-- 第2行 -->
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.chinese_short_name')" label-align="right">
                            {{ scope.row.shipowner.chinese_short_name }}
                          </el-descriptions-item>
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.english_short_name')" label-align="right" :span="2">
                            {{ scope.row.shipowner.english_short_name }}
                          </el-descriptions-item>

                          <!-- 第3行 -->
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.mobile')" label-align="right">
                            {{ scope.row.shipowner.mobile }}
                          </el-descriptions-item>
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.email')" label-align="right" :span="2">
                            {{ scope.row.shipowner.email }}
                          </el-descriptions-item>

                          <!-- 第4行 -->
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.address')" label-align="right" :span="3">
                            {{ scope.row.shipowner.address }}
                          </el-descriptions-item>

                          <!-- 第5行 -->
                          <el-descriptions-item width="200" :label="i18n('business.shipowner.description')" label-align="right" :span="3">
                            {{ scope.row.shipowner.description }}
                          </el-descriptions-item>
                        </el-descriptions>
                      </el-col>
                    </el-row>
                  </div>
                </el-card>

                <el-card class="box-card mt10" shadow="never" v-if="scope.row.route">
                  <template v-slot:header>
                    <div class="clearfix">
                      <span>{{ i18n('business.information.harbor_info') }}</span>
                    </div>
                  </template>
                  <div class="text item">
                    <el-descriptions border>
                      <div v-for="(v, k) in scope.row.route" :key="k">
                        <el-descriptions-item width="200" :label="i18n('business.regular.harbor_code')" label-align="right">
                          <span v-if="v.harbor">{{ v.harbor.english_name }}</span> <span v-if="v.second_harbor_code">/ {{ v.second_harbor_name }}</span> <span v-if="v.third_harbor_code">/ {{ v.third_harbor_name }}</span>
                        </el-descriptions-item>
                        <el-descriptions-item width="200" :label="i18n('business.regular.start_time')" label-align="right">
                          {{ v.start_time }}
                        </el-descriptions-item>
                        <el-descriptions-item width="200" :label="i18n('business.regular.end_time')" label-align="right">
                          {{ v.end_time }}
                        </el-descriptions-item>
                      </div>
                    </el-descriptions>
                  </div>
                </el-card>
              </div>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.regular.category_id')" width="80">
            <template v-slot="scope">
              <span v-if="scope.row.category">{{ scope.row.category.chinese_title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="$t('business.regular.machine_id')" width="100">
            <template v-slot="scope">
              <span v-if="scope.row.machine">{{ scope.row.machine.chinese_title }}</span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('business.regular.shipowner_id')" width="200">
            <template v-slot="scope">
              <span v-if="scope.row.shipowner">{{ scope.row.shipowner.chinese_name }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="voyage_number" :label="i18n('business.regular.voyage_number')" width="100">
          </el-table-column>

          <el-table-column prop="title" :label="i18n('business.regular.title')" min-width="100">
          </el-table-column>

          <el-table-column prop="publish_time" :label="i18n('business.regular.publish_time')" width="100">
          </el-table-column>

          <el-table-column :label="$t('common.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="create_time" :label="i18n('business.information.create_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="200">
            <template v-slot="scope">
              <el-button
                v-if="isAuth('module:business:archived:regular:form')"
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_business_archived_regular_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                v-if="isAuth('module:business:archived:regular:delete')"
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="business-archived-regular-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"
  import { datetime } from "@/utils/filter"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('business/archived/regular')

  const shipownerList = ref([])
  const managerList = ref([])

  const dataForm = reactive([
    'shipowner_id',
    'manager__manager_id',
    'title',
    'status',
    'publish_time',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    handleStatus,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadShipownerList = () => {
    $http({
      url: $http.adornUrl('/business/shipowner/select'),
      method: 'get',
      params: $http.adornParams({}),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        shipownerList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadManagerList = (shipowner_id) => {
    $http({
      url: $http.adornUrl('/business/manager/select'),
      method: 'get',
      params: $http.adornParams({
        shipowner_id: shipowner_id
      }),
    }).then(({ data }) => {
      if (data && data.status === 200) {
        managerList.value = data.data
        dataForm.manager_id = ''
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadShipownerList()
  })
</script>
